<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/18 14:34
 * Modified Time: 2019/7/18 14:34
 * Version: 2.0
 */

namespace App\Modules\Store\Criterias;


use App\Lib\Facades\FLAuth;
use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;
use Illuminate\Support\Facades\DB;

class GetMyLikeStoreCriteria extends BaseCriterias
{
    protected $member_id;

    public function __construct($member_id)
    {
        $this->member_id = $member_id;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $member_id = $this->member_id;

        //先获取用户关注的店铺id
        $store_id_list = DB::table('store_like')
            ->select('store_id')
            ->where('member_id', $member_id)
            ->get()
            ->toArray();
        $store_id_list = $this->getStoreIdList($store_id_list);

        //创建店铺关注查询 用于计算粉丝数
        $store_like_query = DB::table('store_like')
            ->selectRaw('store_id,count(id) as count')
            ->whereIn('store_id', $store_id_list)
            ->groupBy('store_id');


        //创建评论查询 用于计算好评率
        $comment_query = DB::table('order_comment')
            ->selectRaw('store_id,score_store,IF(score_store=5,1,0) as good_score,count(id) as count')
            ->whereIn('store_id', $store_id_list)
            ->groupBy('store_id');


        $select = [
            'hx_store.id',
            'hx_store.name',
            'hx_picture.image as logo',
            'IF(hx_comment.count=0 || hx_comment.count is null,100,hx_comment.good_score/hx_comment.count*100) as good_score', //好评率 如果没有评论则 为100 否则为评分是5的评论数除以所有评论数
            'hx_store_like.count as fans_num'
        ];
        $select = implode($select, ',');

        $model = $model->selectRaw($select)
            ->leftJoin('picture', 'store.logo', 'picture.id')
            ->leftJoin(\DB::raw("({$comment_query->toSql()}) as hx_comment"), function ($join) use ($comment_query) {
                $join->mergeBindings($comment_query)->on('store.id', '=', 'comment.store_id');
            })
            ->leftJoin(\DB::raw("({$store_like_query->toSql()}) as hx_store_like"), function ($join) use ($store_like_query) {
                $join->mergeBindings($store_like_query)->on('store.id', '=', 'store_like.store_id');
            })
            ->with(['storeResource' => function ($query) {
                $select = [
                    'hx_store_resource.id',
                    'hx_store_resource.store_id',
                    'hx_store_resource.resource',
                    'type',
                    'IF(type = 1,0,1) as is_video',
                    'CASE WHEN type = 1 THEN hx_picture.image else hx_video.thumb END as image',
                    'CASE WHEN type = 1 THEN null else hx_video.url END as video',
                ];
                $select = implode($select, ',');

                $query->selectRaw($select)
                    ->leftJoin('picture', function ($join) {
                        $join->on('store_resource.resource', '=', 'picture.id');

                    })
                    ->leftJoin('video', function ($join) {
                        $join->on('store_resource.resource', '=', 'video.id');

                    })
                    ->orderBy('type', 'DESC');
            }])
            ->whereIn('store.id', $store_id_list);

        return $model;
    }

    //对象转数组
    protected function getStoreIdList($array)
    {
        $new_array = [];

        foreach ($array as $key => $value) {
            $value = array_values(get_object_vars($value));
            $new_array[] = $value[0];
        }

        return $new_array;
    }
}